package com.tang.core;

import com.tang.domian.Token;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author Tang
 * @classname UserDetail
 * @description [  ]
 * @date 2021/5/9 11:43
 */
public class UserDetail implements UserDetails {

    private Collection<? extends GrantedAuthority> grantedAuthorities;

    private String username;

    public UserDetail(Token token) {
        List<String> authorityCodes = token.getAuthorityCodes();
        this.grantedAuthorities = authorityCodes.stream()
                .map(SimpleGrantedAuthority::new)
                .collect(Collectors.toList());
        this.username = token.getUsername();
    }

    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        return grantedAuthorities;
    }

    @Override
    public String getPassword() {
        return null;
    }

    @Override
    public String getUsername() {
        return username;
    }

    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    @Override
    public boolean isAccountNonLocked() {
        return true;
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    @Override
    public boolean isEnabled() {
        return true;
    }
}